package com.cljz.peipei.present.activity;

import com.cljz.peipei.base.BaseP;
import com.cljz.peipei.model.net.api.Api;
import com.cljz.peipei.model.net.bean.LoginBean;
import com.cljz.peipei.model.net.bean.ResponseBean;
import com.cljz.peipei.ui.activity.EditSmsCodeActivity;
import com.clzj.peipei.bean.UserInfo;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;

/**
 * Created by apple on 2017/4/18.
 */

public class EditSmsCodeP extends BaseP<EditSmsCodeActivity> {
    public void getLogin(String phone, String code) {
        Api.getResponseInfoAPI().LoginData(phone, code, "sms")
                .compose(XApi.<LoginBean>getApiTransformer())
                .compose(XApi.<LoginBean>getScheduler())
                .compose(getV().<LoginBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<LoginBean>() {
                    @Override
                    protected void onFail(NetError error) {
                        getV().dismissLoadingDialog();
                    }

                    @Override
                    public void onNext(LoginBean loginBean) {
                        if ("0".equals(loginBean.getCode())) {
                            LoginBean.DataBean dataBean = loginBean.getData();
                            putToken(dataBean.getToken());
                            UserInfo userInfo = new UserInfo();
                            userInfo.setToken(dataBean.getToken());
                            userInfo.setBirthday(dataBean.getBirthday());
                            userInfo.setNickname(dataBean.getNickname());
                            userInfo.setSex(dataBean.getSex());
                            userInfo.setUser_id(dataBean.getUser_id());
                            userInfo.setDownload(dataBean.getDownload());
                            updateDB(userInfo);
                            getV().loginSucceed(loginBean.getData());
                        } else {
                            getV().loginFail(loginBean.getMsg());
                        }
                    }
                });
    }

    public void sendSmsCode(String phone) {
        Api.getResponseInfoAPI().sendSmsData(phone, "sms_login")
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(ResponseBean sendSmsCodeBean) {
                        if ("0".equals(sendSmsCodeBean.getCode())) {
                            getV().sendSmsSucceed(sendSmsCodeBean);
                        } else {
                            getV().sendSmsFail(sendSmsCodeBean.getMsg());
                        }
                    }
                });
    }

    private void updateDB(UserInfo userInfo) {
        if (getUserInfo(getToken()) == null) {
            daoSession.getUserInfoDao().deleteAll();
            daoSession.getUserInfoDao().insert(userInfo);
        } else {
            daoSession.getUserInfoDao().update(userInfo);
        }
    }
}
